/*
 * @Author: your name
 * @Date: 2024-04-24 16:27:02
 * @LastEditTime: 2024-04-24 16:35:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\112.路径总和.cpp
 */
/*
 * @lc app=leetcode.cn id=112 lang=cpp
 *
 * [112] 路径总和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool traversal(TreeNode* root, int count){
        if(!root->left && !root->right && count == 0){
            return true;
        }
        if(!root->left && !root->right){
            return false;
        }
        if(root->left){
            if(traversal(root->left, count - root->left->val)) return true;
        }
        if(root->right){
            if(traversal(root->right, count - root->right->val)) return true;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root){
            return false;
        }
        return traversal(root, targetSum - root->val);
    }
};
// @lc code=end

